package cn.lixuan.service.impl;

import cn.lixuan.constants.BaseInfoProperties;
import cn.lixuan.enums.YesOrNo;
import cn.lixuan.mapper.FansMapper;
import cn.lixuan.mapper.FansMapperCustom;
import cn.lixuan.pojo.Fans;
import cn.lixuan.pojo.Vlog;
import cn.lixuan.service.FansService;
import cn.lixuan.utils.PagedGridResult;
import cn.lixuan.vo.FansVO;
import cn.lixuan.vo.VlogerVO;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FansServiceImpl extends BaseInfoProperties implements FansService {

    @Autowired
    private FansMapper fansMapper;

    @Autowired
    private FansMapperCustom fansMapperCustom;

    @Autowired
    private Sid sid;

    @Override
    @Transactional
    public void doFollow(String myId, String vlogerId) {
        String fid = sid.nextShort();

        Fans fans = new Fans();
        fans.setId(fid);
        fans.setFanId(myId);
        fans.setVlogerId(vlogerId);

        // 判断对方是否关注我，如果关注我，那么双方都要互为朋友关系
        Fans vloger = queryFansRelationShip(vlogerId, myId);
        if (vloger != null) {
            fans.setIsFanFriendOfMine(YesOrNo.YES.type);

            vloger.setIsFanFriendOfMine(YesOrNo.YES.type);
            fansMapper.updateByPrimaryKeySelective(vloger);
        } else {
            fans.setIsFanFriendOfMine(YesOrNo.NO.type);
        }

        fansMapper.insert(fans);
    }

    @Override
    @Transactional
    public void doCancel(String myId, String vlogerId) {
        // 判断是否是朋友关系，如果是，则需要取消双方的关系
        Fans fan = queryFansRelationShip(myId, vlogerId);

        if(fan != null && fan.getIsFanFriendOfMine() == YesOrNo.YES.type) {
            // 抹除双方的朋友关系，自己的关系删除即可
            Fans pendingFan = queryFansRelationShip(vlogerId, myId);
            pendingFan.setIsFanFriendOfMine(YesOrNo.NO.type);
            fansMapper.updateByPrimaryKeySelective(pendingFan);
        }

        fansMapper.delete(fan);
    }

    @Override
    public boolean queryDoIFollowVloger(String myId, String vlogerId) {

        Fans pendingFan = queryFansRelationShip(myId, vlogerId);
        if(pendingFan != null) {
            return true;
        }
        return false;
    }

    @Override
    public PagedGridResult queryMyFollows(String myId,
                                          Integer page,
                                          Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("myId", myId);

        PageHelper.startPage(page, pageSize);

        List<VlogerVO> list = fansMapperCustom.queryMyFollows(map);

        return setterPagedGrid(list, page);
    }

    @Override
    public PagedGridResult queryMyFans(String myId, Integer page, Integer pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("myId", myId);

        PageHelper.startPage(page, pageSize);

        List<FansVO> list = fansMapperCustom.queryMyFans(map);

        // 判断是否是我的朋友(互粉互关)
        for (FansVO f : list) {
            String relationship = redis.get(BaseInfoProperties.REDIS_FANS_AND_VLOGGER_RELATIONSHIP + myId + ":" + f.getFanId());

            if(StringUtils.isNotBlank(relationship) && relationship.equalsIgnoreCase("1")) {
                f.setFriend(true);
            }
        }

        return setterPagedGrid(list, page);
    }


    public Fans queryFansRelationShip(String fanId, String vlogerId) {

        Example example = new Example(Fans.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("vlogerId", vlogerId);
        criteria.andEqualTo("fanId", fanId);

        List<Fans> list = fansMapper.selectByExample(example);

        Fans fan = null;

        if(list != null && list.size() > 0 && !list.isEmpty()) {
            fan = (Fans) list.get(0);
        }

        return fan;
    }
}
